home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / asymptopia / Validator.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2003-11-16  |  6.9 KB  |  308 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''
  5. /***************************************************************************
  6.  
  7. \tAuthor \t\t\t:Charles B. Cosse 
  8. \t
  9. \tEmail\t\t\t:ccosse@asymptopia.com
  10. \t\t\t\t\t
  11. \t\t\t\t\t
  12. \tCopyright\t\t:(C) 2002,2003 Asymptopia Software.
  13. \t
  14.  ***************************************************************************/
  15. /***************************************************************************
  16.                           Validator.py
  17.  
  18.  ***************************************************************************/
  19.  
  20. /***************************************************************************
  21.  *                                                                         *
  22.  *   This program is free software; you can redistribute it and/or modify  *
  23.  *   it under the terms of the GNU General Public License as published by  *
  24.  *   the Free Software Foundation; either version 2 of the License, or     *
  25.  *   (at your option) any later version. (Please note that if you use this *
  26.  *   code you must give credit by including the Author and Copyright       *
  27.  *   info at the top of this file).                                        *
  28.  ***************************************************************************/
  29. '''
  30. import os
  31. import pygame
  32. from pygame.locals import *
  33.  
  34. class Validator:
  35.     '''Validator has game model.
  36. \t
  37. \t'''
  38.     
  39.     def __init__(self, board, game):
  40.         self.board = board
  41.         self.game = game
  42.  
  43.     
  44.     def validate(self, submission):
  45.         if len(submission) == 0:
  46.             return 0
  47.         
  48.         board = self.board
  49.         MinM = board.M
  50.         MinN = board.N
  51.         for spot in submission:
  52.             if spot.M <= MinM and spot.N <= MinN:
  53.                 MinM = spot.M
  54.                 MinN = spot.N
  55.             
  56.         
  57.         try_above = 1
  58.         while 1:
  59.             if MinN == 0:
  60.                 break
  61.             elif board.get_spotMN(MinM, MinN - 1).guest != None:
  62.                 MinN = MinN - 1
  63.                 try_above = 0
  64.             else:
  65.                 break
  66.         while 1:
  67.             if not try_above:
  68.                 break
  69.             elif MinM == 0:
  70.                 break
  71.             elif board.get_spotMN(MinM - 1, MinN).guest != None:
  72.                 MinM = MinM - 1
  73.             else:
  74.                 break
  75.         MaxM = 0
  76.         MaxN = 0
  77.         for spot in submission:
  78.             if spot.M >= MaxM and spot.N >= MaxN:
  79.                 MaxM = spot.M
  80.                 MaxN = spot.N
  81.             
  82.         
  83.         try_below = 1
  84.         while 1:
  85.             if MaxN == board.N - 1:
  86.                 break
  87.             elif board.get_spotMN(MaxM, MaxN + 1).guest != None:
  88.                 MaxN = MaxN + 1
  89.                 try_below = 0
  90.             else:
  91.                 break
  92.         while 1:
  93.             if not try_below:
  94.                 break
  95.             elif MaxM == board.M - 1:
  96.                 break
  97.             elif board.get_spotMN(MaxM + 1, MaxN).guest != None:
  98.                 MaxM = MaxM + 1
  99.             else:
  100.                 break
  101.         if MaxM - MinM > 0 and MaxN - MinN > 0:
  102.             return 0
  103.         elif MaxM - MinM > 0:
  104.             exprtype = 'col'
  105.         elif MaxN - MinN > 0:
  106.             exprtype = 'row'
  107.         elif MaxN - MinN == 0:
  108.             return 0
  109.         else:
  110.             return 0
  111.         if exprtype == 'row':
  112.             for idx in range(MinN, MaxN + 1):
  113.                 this_idx_okay = 0
  114.                 for spot in submission:
  115.                     if spot.N == idx:
  116.                         this_idx_okay = 1
  117.                     
  118.                 
  119.                 if board.get_spotMN(MinM, idx).guest != None:
  120.                     this_idx_okay = 1
  121.                 
  122.                 if this_idx_okay == 0:
  123.                     return 0
  124.                 
  125.             
  126.         elif exprtype == 'col':
  127.             for idx in range(MinM, MaxM + 1):
  128.                 this_idx_okay = 0
  129.                 for spot in submission:
  130.                     if spot.M == idx:
  131.                         this_idx_okay = 1
  132.                     
  133.                 
  134.                 if board.get_spotMN(idx, MinN).guest != None:
  135.                     this_idx_okay = 1
  136.                 
  137.                 if this_idx_okay == 0:
  138.                     return 0
  139.                 
  140.             
  141.         
  142.         expr = ''
  143.         if exprtype == 'row':
  144.             for idx in range(MinN, MaxN + 1):
  145.                 if board.get_spotMN(MinM, idx).guest != None:
  146.                     expr = expr + board.get_spotMN(MinM, idx).guest.str_val
  147.                     continue
  148.                 else:
  149.                     for spot in submission:
  150.                         if spot.N == idx:
  151.                             expr = expr + spot.guest.str_val
  152.                             continue
  153.                         
  154.                     
  155.             
  156.         elif exprtype == 'col':
  157.             for idx in range(MinM, MaxM + 1):
  158.                 if board.get_spotMN(idx, MinN).guest != None:
  159.                     expr = expr + board.get_spotMN(idx, MinN).guest.str_val
  160.                 else:
  161.                     for spot in submission:
  162.                         if spot.M == idx:
  163.                             expr = expr + spot.guest.str_val
  164.                         
  165.                     
  166.             
  167.         
  168.         if expr.count('=') == 0:
  169.             return 0
  170.         
  171.         for even_value in [
  172.             0,
  173.             2,
  174.             4,
  175.             6,
  176.             8,
  177.             10,
  178.             12,
  179.             14,
  180.             16,
  181.             18,
  182.             20]:
  183.             length = len(expr)
  184.             if expr.count('10'):
  185.                 length = length - expr.count('10')
  186.             
  187.             if expr.count('11'):
  188.                 length = length - expr.count('11')
  189.             
  190.             if expr.count('12'):
  191.                 length = length - expr.count('12')
  192.             
  193.             if expr.count('13'):
  194.                 length = length - expr.count('13')
  195.             
  196.             if expr.count('14'):
  197.                 length = length - expr.count('14')
  198.             
  199.             if expr.count('15'):
  200.                 length = length - expr.count('15')
  201.             
  202.             if expr.count('20'):
  203.                 length = length - expr.count('20')
  204.             
  205.             if length == even_value:
  206.                 return 0
  207.             
  208.         
  209.         expr = expr.replace('=', '==')
  210.         
  211.         try:
  212.             rval = eval(expr)
  213.         except:
  214.             return 0
  215.  
  216.         if rval == 0:
  217.             return 0
  218.         
  219.         head_spot = None
  220.         for spot in submission:
  221.             if spot.M == MinM and spot.N == MinN:
  222.                 head_spot = spot
  223.             
  224.         
  225.         if not head_spot:
  226.             head_spot = board.get_spotMN(MinM, MinN)
  227.         
  228.         head_spot.AMHEAD = 1
  229.         if exprtype == 'row':
  230.             head_spot.AMROWEXPR = 1
  231.             head_spot.ROWEXPRLENGTH = (MaxN - MinN) + 1
  232.         
  233.         if exprtype == 'col':
  234.             head_spot.AMCOLEXPR = 1
  235.             head_spot.COLEXPRLENGTH = (MaxM - MinM) + 1
  236.         
  237.         if exprtype == 'row':
  238.             multiplier = 1
  239.             score = 0
  240.             for nidx in range(MinN, MaxN + 1):
  241.                 spot2check = board.get_spotMN(MinM, nidx)
  242.                 if spot2check.TYPE == '2XL':
  243.                     score = score + 2
  244.                 elif spot2check.TYPE == '2XW':
  245.                     score = score + 1
  246.                     multiplier = multiplier * 2
  247.                 elif spot2check.TYPE == '3XL':
  248.                     score = score + 3
  249.                 elif spot2check.TYPE == '3XW':
  250.                     score = score + 1
  251.                     multiplier = multiplier * 3
  252.                 else:
  253.                     score = score + 1
  254.             
  255.             score = score * multiplier
  256.             self.game.playerscore = self.game.playerscore + score
  257.             for n in range(MinN + 1, MaxN + 1):
  258.                 if board.check4guest(MinM, n):
  259.                     spot2check = board.get_spotMN(MinM, n)
  260.                     if spot2check.AMHEAD and spot2check.AMROWEXPR:
  261.                         spot2check.AMROWEXPR = 0
  262.                         spot2check.ROWEXPRLENGTH = 0
  263.                         if spot2check.AMCOLEXPR:
  264.                             pass
  265.                         else:
  266.                             spot2check.AMHEAD = 0
  267.                     
  268.                 
  269.             
  270.         
  271.         if exprtype == 'col':
  272.             multiplier = 1
  273.             score = 0
  274.             for midx in range(MinM, MaxM + 1):
  275.                 spot2check = board.get_spotMN(midx, MinN)
  276.                 if spot2check.TYPE == '2XL':
  277.                     score = score + 2
  278.                 elif spot2check.TYPE == '2XW':
  279.                     score = score + 1
  280.                     multiplier = multiplier * 2
  281.                 elif spot2check.TYPE == '3XL':
  282.                     score = score + 3
  283.                 elif spot2check.TYPE == '3XW':
  284.                     score = score + 1
  285.                     multiplier = multiplier * 3
  286.                 else:
  287.                     score = score + 1
  288.             
  289.             score = score * multiplier
  290.             self.game.playerscore = self.game.playerscore + score
  291.             for m in range(MinM + 1, MaxM + 1):
  292.                 if board.check4guest(m, MinN):
  293.                     spot2check = board.get_spotMN(m, MinN)
  294.                     if spot2check.AMHEAD and spot2check.AMCOLEXPR:
  295.                         spot2check.AMCOLEXPR = 0
  296.                         spot2check.COLEXPRLENGTH = 0
  297.                         if spot2check.AMROWEXPR:
  298.                             pass
  299.                         else:
  300.                             spot2check.AMHEAD = 0
  301.                     
  302.                 
  303.             
  304.         
  305.         return 1
  306.  
  307.  
  308.